package com.han.adbconnect;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.han.adbconnect.utils.ShellUtils;

import java.util.Calendar;
import java.util.List;

/**
 * @author Administrator on 2025-02-26.
 * <p>
 * 站在顶峰 看世界
 * 落到谷底 思人生
 */
public class TimeChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null && (action.equals(Intent.ACTION_TIME_TICK))) {
            Calendar mCalendar = Calendar.getInstance();
            int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
            int minute = mCalendar.get(Calendar.MINUTE);
            int startTime = 0;

            if (hour == 14 && minute == 56) {
                new Thread(() -> {
                    ShellUtils.CommandResult commandResult = ShellUtils.execCommand(new String[]{"su", "0", "reboot"}, true);
                }).start();
            }
        }
    }

    public boolean isAppForeground(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfo = am.getRunningAppProcesses();
        if (processInfo != null) {
            for (ActivityManager.RunningAppProcessInfo aInfo : processInfo) {
                if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : aInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            // 当前应用处于前台
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}
